package com.lineage.data.npc.quest2;

import com.lineage.data.executor.NpcExecutor;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1SkillMode;
import com.lineage.server.model.skill.skillmode.SkillMode;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1QuestUser;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.L1SpawnUtil;
import com.lineage.server.world.WorldQuest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.TimerTask;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Npc_DragonB3 extends NpcExecutor {
	private static final Log _log = LogFactory.getLog(Npc_DragonB3.class);

	public static final Map<Integer, checkDragonTimer3> _timer = new HashMap<>();

	public static NpcExecutor get() {
		return new Npc_DragonB3();
	}

	public int type() {
		return 3;
	}

	public void talk(L1PcInstance pc, L1NpcInstance npc) {
		try {
			if (pc.hasSkillEffect(4010))
				pc.removeSkillEffect(4010);
			if (pc.hasSkillEffect(4009))
				pc.removeSkillEffect(4009);
			if (pc.hasSkillEffect(4018))
				return;
			pc.sendPacketsX8((ServerBasePacket) new S_SkillSound(pc.getId(), 7683));
			SkillMode mode = L1SkillMode.get().getSkill(4018);
			if (mode != null)
				try {
					mode.start(pc, null, null, 2400);
				} catch (Exception e) {
					_log.error(e.getLocalizedMessage(), e);
				}
		} catch (Exception e2) {
			_log.error(e2.getLocalizedMessage(), e2);
		}
		if (pc.hasSkillEffect(4018)) {
			try {
				L1QuestUser quest = WorldQuest.get().get(pc.get_showId());
				if (quest != null) {
					if (!quest.is_SpawnedDragon()) {
						checkDragonTimer3 timer = new checkDragonTimer3(npc.getMapId(), quest);
						timer.begin();
						quest.set_SpawnedDragon(true);
					}
					L1Location loc = (new L1Location(32844, 32887, npc.getMapId())).randomLocation(5, false);
					L1Teleport.teleport(pc, loc.getX(), loc.getY(), npc.getMapId(), 4, true);
				}
			} catch (Exception e) {
				_log.error(e.getLocalizedMessage(), e);
			}
		}

	}

	public void action(L1PcInstance pc, L1NpcInstance npc, String cmd, long amount) {
	}

	private class checkDragonTimer3 extends TimerTask {
		private final int mapId;

		private final L1QuestUser quest;

		public checkDragonTimer3(int mapId, L1QuestUser quest) {
			this.mapId = mapId;
			this.quest = quest;
		}

		public void run() {
			cancel();
			try {
				sendServerMessage(1755);
				Thread.sleep(2000L);
				sendServerMessage(1756);
				Thread.sleep(2000L);
				sendServerMessage(1757);
				Thread.sleep(2000L);
				sendServerMessage(1758);
				L1Location loc = (new L1Location(32846, 32877, this.mapId)).randomLocation(5, true);
				L1SpawnUtil.spawn(97204, loc, (new Random()).nextInt(8), this.quest.get_id());
			} catch (Exception exception) {

			} finally {
				Npc_DragonB3._timer.remove(Integer.valueOf(this.quest.get_id()));
			}
		}

		private final void sendServerMessage(int msgid) {
			if (!this.quest.pcList().isEmpty()) {
				Iterator<L1PcInstance> iterator = this.quest.pcList().iterator();
				while (iterator.hasNext()) {
					L1PcInstance pc = iterator.next();
					pc.sendPackets((ServerBasePacket) new S_ServerMessage(msgid));
				}
			}
		}

		public final void begin() {
			GeneralThreadPool.get().schedule(this, 30000L);
		}
	}
}
